package other;

import handlers.AiHandler;

import java.util.List;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

public class LifeInABox extends BasicGame{
	
    public LifeInABox(){
        super("Life in a box");
    }

    public static void main(String[] arguments){
    	
        try{
            AppGameContainer app = new AppGameContainer(new LifeInABox());
            app.setDisplayMode(1366, 768, false);
            app.setShowFPS(true);
            app.setVSync(false);
            app.start();
        }catch (SlickException e){
            e.printStackTrace();
        }
    }
        

    @Override
    public void init(GameContainer container) throws SlickException{
    	
    	//To later be able to paint grid
    	World.setGridSize(((float)container.getWidth()-2)/World.getNumberofgrids().x, ((float)container.getHeight()-2)/World.getNumberofgrids().y);
    	
    	for (int i = 0; i <5000; i++) {
    		World.addShape(new RandomLife(container.getWidth()/2, container.getHeight()/2, 2, 2));
		}    	
    }
    
    @Override
    public void update(GameContainer container, int delta) throws SlickException{
    	Input input = container.getInput();
    	
    	Time.tick(delta);

    	AiHandler.update(delta);
    	
    }

    public void render(GameContainer container, Graphics g) throws SlickException{

    	g.setLineWidth(1);
    	g.setAntiAlias(false);
    	
    	//Draw grid
    	g.setColor(new Color(40,40,40));
    	drawGrid(20, 20, container, g);

    	//Draw Life
    	g.setColor(RandomLife.getColor());
    	List<Life> shapes = World.getLives();
    	for (int i = 0; i < shapes.size(); i++) {
			g.draw(shapes.get(i));
		}
    	
    	//Draw Food
    	g.setColor(Color.green);
    	List<Food> foods = World.getFood();
    	for (int i = 0; i < foods.size(); i++) {
    		Food food = foods.get(i);
    		
    		//Inline
    		g.setColor(new Color(0,255,0,220));
    		g.drawOval(food.getCenterX()-10, food.getCenterX()-10, 20, 20);
    		
    		//Fill inside
    		g.setColor(new Color(0,255,0,20));
    		g.fillOval(food.getCenterX()-40, food.getCenterX()-40, 80, 80);
    		
    		//Outline
    		g.setColor(new Color(0,255,0,80));
    		g.drawOval(food.getCenterX()-40, food.getCenterX()-40, 80, 80);
		}

    	//Draw Time
    	g.setColor(Color.white);
    	g.drawString(Time.getTime(), 10, 30);
    }

    private void drawGrid(int i, int j, GameContainer container, Graphics g) {
    	
    	for (float x = 1; x < container.getWidth(); x+=World.getGridSize().x) {
    		g.drawLine((int)x, 0, (int)x, container.getHeight());
		}
    	
    	for (float y = 0; y < container.getHeight(); y+=World.getGridSize().y) {
			g.drawLine(0, (float)y, container.getWidth(), (float)y);
		}
    	
	}
}